package net.wanho.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.entity.Goods;
import net.wanho.shop.mapper.GoodsMapper;
import net.wanho.shop.service.GoodsService;
import net.wanho.shop.vo.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-09-24 9:17
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl implements GoodsService {

    private final GoodsMapper goodsMapper;

    @Override
    public Page<Goods> findPage(Integer pageNum, Integer pageSize, Goods goods) {
        PageHelper.startPage(pageNum, pageSize);

        List<Goods> list = goodsMapper.selectList(goods);

        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        Page<Goods> page = new Page<>(pageInfo.getTotal(), pageInfo.getList());

        return page;
    }

    @Override
    public void save(Goods goods) {
        if (ObjectUtils.isEmpty(goods.getId())){
            goodsMapper.insertSelective(goods);
        }else{
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
    }

    @Override
    public Goods findById(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void remove(Integer id) {
        goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatch(List<Integer> ids) {
        goodsMapper.deleteBatch(ids);
    }
}
